//
//  ARENWaterFlowLayout.m
//  FlowerRainGround
//
//  Created by 阿仁欧巴 on 16/4/22.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ARENWaterFlowLayout.h"


/** 默认的列数 */
static const NSInteger defaultColumnCount = 3;
/** 每一列之间的间距 */
static const CGFloat defaultColumnMargin = 10;
/** 每一行之间的间距 */
static const CGFloat defaultRowMargin = 10;
/** 边缘间距 */
static const UIEdgeInsets defaultEdgeInsets = {10, 10, 10, 10};


@interface ARENWaterFlowLayout ()

/** 存放所有cell的布局属性 */
@property (nonatomic, strong) NSMutableArray *attrsArray;
/** 存放所有列的当前高度 */
@property (nonatomic, strong) NSMutableArray *columnHeights;
/** 内容的高度 */
@property (nonatomic, assign) CGFloat contentHeight;

- (CGFloat)rowMargin; //行间距
- (CGFloat)columnMargin; //列间距
- (NSInteger)columnCount; // 列数
- (UIEdgeInsets)edgeInsets; //边距


@end

@implementation ARENWaterFlowLayout


//实现间距的边距的getter方法
- (CGFloat)rowMargin
{
    if ([self.delegate respondsToSelector:@selector(rowMarginInWaterflowLayout:)]) {
        return [self.delegate rowMarginInWaterflowLayout:self];
    } else {
        return defaultRowMargin;
    }
}

- (CGFloat)columnMargin
{
    if ([self.delegate respondsToSelector:@selector(columnMarginInWaterflowLayout:)]) {
        return [self.delegate columnMarginInWaterflowLayout:self];
    } else {
        return defaultColumnMargin;
    }
}

- (NSInteger)columnCount
{
    if ([self.delegate respondsToSelector:@selector(columnCountInWaterflowLayout:)]) {
        return [self.delegate columnCountInWaterflowLayout:self];
    } else {
        return defaultColumnCount;
    }
}

- (UIEdgeInsets)edgeInsets
{
    if ([self.delegate respondsToSelector:@selector(edgeInsetsInWaterflowLayout:)]) {
        return [self.delegate edgeInsetsInWaterflowLayout:self];
    } else {
        return defaultEdgeInsets;
    }
}







#pragma mark ---懒加载---

- (NSMutableArray *)columnHeights
{
    if (!_columnHeights) {
        _columnHeights = [NSMutableArray array];
    }
    return _columnHeights;
}

- (NSMutableArray *)attrsArray
{
    if (!_attrsArray) {
        _attrsArray = [NSMutableArray array];
    }
    return _attrsArray;
}

#pragma mark --- 1.初始化布局------

- (void)prepareLayout
{
    [super prepareLayout];
    self.contentHeight = 0;
//清除以前计算的所有高度
    [self.attrsArray removeAllObjects];
    //开始创建每个cell对应的布局属性
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (NSInteger i = 0; i < count; i++) {
        //创建位置
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        //获取indexPath位置上cell对应的布局属性
        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
        [self.attrsArray addObject:attrs];
    }

}


#pragma  mark ---- 2.决定cell的排布

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return self.attrsArray;
}

#pragma mark --- 3.返回indexPath位置cell对应的布局属性 -----

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //创建布局属性
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    //collectionView的宽度
    CGFloat collectionViewWidth = self.collectionView.frame.size.width;
    
    //设置布局属性的frame
    CGFloat width = ((collectionViewWidth - self.edgeInsets.left - self.edgeInsets.right - (self.columnCount - 1) * self.columnMargin) / self.columnCount);
    CGFloat height = [self.delegate waterFlowLayout:self heightForItemAtIndex:indexPath.item itemWidth:width];
    
    //找出最短的那一列 (1,2,3列)
    NSInteger destColumn = 0;
    CGFloat minColumnHeight = [self.columnHeights[0] doubleValue];
    for (NSInteger i = 1; i < self.columnCount; i++) {
        //获取第i列的高度
        CGFloat columnHeight = [self.columnHeights[i] doubleValue];
        if (minColumnHeight > columnHeight) {
            minColumnHeight = columnHeight;
            destColumn = i;
        }
    }
    CGFloat x = self.edgeInsets.left + destColumn * (width + self.columnMargin);
    CGFloat y = minColumnHeight;
    if (y != self.edgeInsets.top) {
        y += self.rowMargin;
    }
    attrs.frame = CGRectMake(x, y, width, height);
    
    //更新最短的那列高度
    self.columnHeights[destColumn] = @(CGRectGetMaxY(attrs.frame));
    
    //记录内容的高度
    CGFloat columnHeight = [self.columnHeights[destColumn] doubleValue]; //最短列的高
    
    if (self.contentHeight < columnHeight) {
        self.contentHeight = columnHeight;
    }
    return attrs;

}

//设置瀑布流的大小
- (CGSize)collectionViewContentSize
{
    /*
         CGFloat maxColumnHeight = [self.columnHeights[0] doubleValue];
         for (NSInteger i = 0; i < self.columnCount; i++) {
         //第i列的高度
         CGFloat columnHeight = [self.columnHeights[i] doubleValue];
         if (maxColumnHeight < columnHeight) {
         maxColumnHeight = columnHeight;
         }
         }

     */
    return CGSizeMake(0, self.contentHeight + self.edgeInsets.bottom);

}





























































@end
